package com.hw.backapi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.hw.backapi.service.IMemberService;
import com.hw.model.entity.InvitorPO;
import com.hw.model.entity.MemberPO;
import com.hw.model.mapper.MemberMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.model.vo.member.MemberInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-03-09
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberPO> implements IMemberService {

    @Autowired
    Cache caffeineCacheObject;

    @Override
    public MemberPO getMemberInfo(Long fid, Long clientId) {
        if( fid == null || clientId == null ){
            return null;
        }
        return (MemberPO) caffeineCacheObject.get("member" + fid.toString() + clientId.toString() , k -> {
            LambdaQueryWrapper<MemberPO> query = new LambdaQueryWrapper<>();
            query.eq(MemberPO::getClientId,clientId);
            query.eq(MemberPO::getFid,fid);
            query.last("limit 1");
            return baseMapper.selectOne(query);
        });

    }

    @Override
    public MemberPO getMemberMasterInfo(Long clientId) {
        if( clientId == null ){
            return null;
        }
        LambdaQueryWrapper<MemberPO> query = new LambdaQueryWrapper<>();
        query.eq(MemberPO::getClientId,clientId);
        query.eq(MemberPO::getIsMaster,1);
        query.last("limit 1");
        return baseMapper.selectOne(query);
    }

    @Override
    public List<MemberPO> getMemberList(List<String> fids) {
        if( CollectionUtil.isEmpty(fids) ){
            return Collections.emptyList();
        }

        LambdaQueryWrapper<MemberPO> query = new LambdaQueryWrapper<>();
        query.in(MemberPO::getFid,fids);
        return baseMapper.selectList(query);
    }

    @Override
    public List<MemberInfoVO> getMemberList(Long fid) {
        if( null == fid ){
            return Collections.emptyList();
        }
        return this.baseMapper.getMemberList(fid);
    }
}
